package com.xs.tool.controller.api;

import com.xs.tool.common.base.BasicController;
import com.xs.tool.common.encryption.Base64Utils;
import com.xs.tool.common.encryption.RsaUtil;
import com.xs.tool.common.jwt.JwtUtil;
import com.xs.tool.common.result.Response;
import com.xs.tool.common.result.Result;
import com.xs.tool.common.result.ResultsCode;
import com.xs.tool.model.User;
import com.xs.tool.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "登录")
@RestController
@RequestMapping("api/login")
public class LoginController extends BasicController {

    @Autowired
    UserService userService;

    /**
     * @return
     * @note save
     * @date 202011070936
     * @Author xs
     */
    @RequestMapping(value = "/login", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "登录", notes = "login")
    @ResponseBody
    public Result login(
            @RequestParam(required = true, value = "userName") String userName,
            @RequestParam(required = false, value = "rememberMe") String rememberMe,
            @RequestParam(required = true, value = "password") String password
    ) {
        try {
            if (null == userName || userName.equals("")) {
                return Response.error(ResultsCode.LOGIN_UNAME);
            }
            if (null == password || password.equals("")) {
                return Response.error(ResultsCode.LOGIN_PWD);
            }
            //对密码进行解密
            password = password.replaceAll("%2B", "+");
            //获取私钥
            String privateKey = RsaUtil.LOGIN_PRIKEY;
            byte[] decryptByPrivateKey = RsaUtil
                    .decryptByPrivateKey(Base64Utils.decode(password), privateKey);
            //解密后的密码
            String decodePassword = new String(decryptByPrivateKey);
            //匹配数据库
            User lgUser = userService.findByName(userName);
            if (null == lgUser) {
                return Response.error(ResultsCode.LOGIN_NOUNAME);
            }
            if (decodePassword.equals(lgUser.getPassword())) {
                String token = JwtUtil.getToken(lgUser);
                if (null != token && !token.equals("")) {
                    lgUser.setToken(token);
                    //修改用户
                    int up = userService.updateUser(token, lgUser.getId());
                    return Response.success(lgUser);
                }
            } else {
                return Response.error(ResultsCode.LOGIN_PWD_E);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Response.error("登录失败");
    }

    /**
     * @param userName  用户名
     * @param password  密码
     * @param verifCode 验证码
     * @param email     邮箱
     * @return
     * @date 202011070936
     * @Author xs
     */
    @RequestMapping(value = "/register", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "注册", notes = "注册")
    @ResponseBody
    public Result register(
            @RequestParam(required = true, value = "userName") String userName,
            @RequestParam(required = true, value = "password") String password,
            @RequestParam(required = true, value = "verifCode;") String verifCode,
            @RequestParam(required = true, value = "email") String email
    ) {
        try {
            if (null == userName || userName.equals("")) {
                return Response.error(ResultsCode.LOGIN_UNAME);
            }
            if (null == password || password.equals("")) {
                return Response.error(ResultsCode.LOGIN_PWD);
            }
            if (null == verifCode || verifCode.equals("")) {
                return Response.error(ResultsCode.LOGIN_YZM);
            }
            if (null == email || email.equals("")) {
                return Response.error(ResultsCode.LOGIN_EMAIL);
            }
            //匹配数据库
            User lgUser = userService.findByName(userName);
            if (null == lgUser) {
                //对密码进行解密
                password = password.replaceAll("%2B", "+");
                //获取私钥
                String privateKey = RsaUtil.LOGIN_PRIKEY;
                byte[] decryptByPrivateKey = RsaUtil
                        .decryptByPrivateKey(Base64Utils.decode(password), privateKey);
                //解密后的密码
                String decodePassword = new String(decryptByPrivateKey);
                //新增用户
            } else {
                return Response.error(ResultsCode.LOGIN_HAVE_UNAME);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Response.error("登录失败");
    }
}
